package com.FH.organization.suggestion;

import com.FH.util.AccessDB;
import com.FH.util.ResultDTO;
import com.FH.util.warnSrc;
import org.codehaus.jettison.json.JSONException;
import org.codehaus.jettison.json.JSONObject;

import javax.ejb.Stateless;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * Created by Administrator on 2017/1/6.
 */
@Stateless
public class SuggestionDAOImpl extends AccessDB implements SuggestionDAO{

    /**
     * 查询处理意见信息
     * @param search
     * @param page
     * @param size
     * @return
     */
    @Override
    public ResultDTO getSuggestionInfo(String search, String page, String size) {
        ResultDTO resultDTO = new ResultDTO();

        try {
            JSONObject json = new JSONObject(search);
            String existName = "0";

            //先查询数据库中是否存在字段
            String sqlexit = "select count(2) from suggestion  where 1=1 ";

            String sql_w = "";//查询条件sql组合
            String sql_page = "";//分页组合

            Integer page_ok = Integer.parseInt(page);//将定义page、size的String类型转换成Integer类型
            Integer size_ok = Integer.parseInt(size);

            if (page.equals("1")) {
                sql_page = " limit 0," + size + "";
            } else {
                sql_page = " limit " + ((page_ok - 1) * size_ok) + "," + size + "";
            }

            if (json.has("form_id")) {
                sql_w += " AND form_id ='" + json.getString("form_id") + "'";
            }
            if (json.has("id")) {
                sql_w += " AND id like '%" + json.getString("id") + "%'";
            }


            String sql_ok = sqlexit + sql_w + sql_page;//组合好的sql语句
            existName = getReportingToOne(sqlexit + sql_w);

            List<String[]> seresult = new ArrayList<String[]>();

            List<SuggestionShowDTO> result = new ArrayList<SuggestionShowDTO>();

            if (!existName.equals("0")) {
                String sql = "select id, sug_desc, form_id, process_suggestion,process_suggestion FROM suggestion where 1=1  " + sql_w + " ORDER BY create_time ASC" + sql_page;
                //     String sql ="select * from finance_budget" ;
                seresult = this.getReportingTo(sql);

                //构建返回对象
                for (int i = 0; i < seresult.size(); i++) {

                    SuggestionShowDTO fbs = new SuggestionShowDTO();
                    fbs.setId(seresult.get(i)[1]);
                    fbs.setDesc(seresult.get(i)[2]);
                    fbs.setForm_id(seresult.get(i)[3]);
                    fbs.setProcess_suggestion(seresult.get(i)[4]);
                    fbs.setCreate_time(seresult.get(i)[5]);
                    fbs.setStep(String.valueOf(i+1));
                    result.add(fbs);
                }
            }
            if (seresult.size() != 0) {
                resultDTO.setStatus("90000");
                resultDTO.setDescritpion(warnSrc.get_success);
            } else {
                resultDTO.setStatus("90001");
                resultDTO.setDescritpion(warnSrc.get_fail);
            }
            resultDTO.setStr(result.size() + "");
            resultDTO.setValues(result);

        } catch (JSONException e) {
            e.printStackTrace();

        }
        return resultDTO;
    }

    /**
     * 添加处理意见信息
     * @param suggestionPullDTO
     * @return
     */
    @Override
    public ResultDTO addSuggestionInfo(SuggestionPullDTO suggestionPullDTO) {
        ResultDTO resultDTO = new ResultDTO();//构建返回对象

        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式

        String uuid = UUID.randomUUID().toString().trim().replaceAll("-", "");
        String time = simpleDateFormat.format(new Date());

        String QUERY = "";

        QUERY += "insert into suggestion(id,sug_desc,process_suggestion,form_id,create_time) values" +
                " ('" + uuid + "','" + suggestionPullDTO.getDesc() + "','" + suggestionPullDTO.getProcess_suggestion() + "','"
                + suggestionPullDTO.getForm_id() + "','" + time + "')";

        int result = this.addReportingTo(QUERY);
        // 判断SQl是否执行成功
        if (result != 0) {
            resultDTO.setStatus("90000");//
            resultDTO.setDescritpion(warnSrc.add_success);
        } else {
            resultDTO.setStatus("90001");//
            resultDTO.setDescritpion(warnSrc.add_fail);
        }
        return resultDTO;
    }
}
